package com.jrx.anytxn.param.service.account.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.account.AccountingCheckConfigReq;
import com.jrx.anytxn.param.dto.account.AccountingCheckConfigRes;
import com.jrx.anytxn.param.dto.account.SubjectAccountConfigReq;
import com.jrx.anytxn.param.dto.account.SubjectAccountConfigRes;
import com.jrx.anytxn.param.entity.GaAccountingCheckConfig;
import com.jrx.anytxn.param.entity.GaSubjectAccountConfig;
import com.jrx.anytxn.param.mapper.ext.ExtGaAccountingCheckConfigMapper;
import com.jrx.anytxn.param.mapper.ext.ExtGaSubjectAccountConfigMapper;
import com.jrx.anytxn.param.service.account.IAccountingCheckConfigService;
import com.jrx.anytxn.param.service.account.ISubjectAccountConfigService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 总分核对配置service实现
 *
 * @author yf
 * date 2019/12/24 0024
 * @version V1.0
 **/
@Service
@CacheConfig(cacheNames = "ACCOUNTING_CHECK")
public class AccountingCheckConfigServiceImpl implements IAccountingCheckConfigService {

    private static final Logger logger = LoggerFactory.getLogger(AccountingCheckConfigServiceImpl.class);

    @Resource
    private ExtGaAccountingCheckConfigMapper extGaAccountingCheckConfigMapper;


    @Autowired
    private ISegmentService segmentService;

    @Override
    public TxnPage<AccountingCheckConfigRes> getAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<AccountingCheckConfigRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<GaAccountingCheckConfig> gaAccountingCheckConfigList = extGaAccountingCheckConfigMapper.selectByTenId(tenantId);
        if (CollectionUtils.isEmpty(gaAccountingCheckConfigList)) {
            logger.error("查询的总分核对配置表列表为空");
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<AccountingCheckConfigRes> res = BeanMapping.copyList(gaAccountingCheckConfigList, AccountingCheckConfigRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public AccountingCheckConfigRes getByTenIdIdAndProductId(String tenantId, String productId) throws TxnException {
        if (StringUtils.isBlank(tenantId)||StringUtils.isBlank(productId)) {
            logger.error("租户ID或产品ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        GaAccountingCheckConfig gaAccountingCheckConfig = extGaAccountingCheckConfigMapper.selectByProductIdAndTenantId(productId,tenantId);
        return BeanMapping.copy(gaAccountingCheckConfig, AccountingCheckConfigRes.class);
    }

    @Cacheable(key = "'accounting_check_'+#tenantId+'_'+#productId")
    @Override
    public GaAccountingCheckConfig findByProductIdAndTenantId(String productId,String tenantId) {
        GaAccountingCheckConfig gaAccountingCheckConfig = extGaAccountingCheckConfigMapper.selectByProductIdAndTenantId(productId,tenantId);
        return gaAccountingCheckConfig;
    }

    @Override
    public AccountingCheckConfigRes addAccountingCheckConfig(AccountingCheckConfigReq accountingCheckConfigReq) throws TxnException {
        try {
            accountingCheckConfigReq.setId(segmentService.getId(Constant.GA_ACCOUNTING_CHECK_CONFIG));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        GaAccountingCheckConfig gaAccountingCheckConfig = BeanMapping.copy(accountingCheckConfigReq, GaAccountingCheckConfig.class);
        /*根据索引查看是否存在此信息*/
        GaAccountingCheckConfig table = extGaAccountingCheckConfigMapper.selectByProductIdAndTenantId(accountingCheckConfigReq.getProductId(),accountingCheckConfigReq.getTenantId());
        if (table != null) {
            logger.error("增加的总分核对配置信息已存在");
            throw new TxnException(TxnRespCode.DATA_EXISTS, TxnRespCode.DATA_EXISTS.getMsg());
        }
        gaAccountingCheckConfig.setRecordVersionNumber(Constant.VERSION_NUMBER);

        extGaAccountingCheckConfigMapper.insertSelective(gaAccountingCheckConfig);
        return BeanMapping.copy(gaAccountingCheckConfig, AccountingCheckConfigRes.class);
    }

    @CacheEvict(key = "'accounting_check_'+#accountingCheckConfigReq.tenantId+'_'+#accountingCheckConfigReq.productId")
    @Override
    public AccountingCheckConfigRes modifyAccountingCheckConfig(AccountingCheckConfigReq accountingCheckConfigReq){
        GaAccountingCheckConfig gaAccountingCheckConfig = BeanMapping.copy(accountingCheckConfigReq, GaAccountingCheckConfig.class);
        extGaAccountingCheckConfigMapper.updateByTenIdAndProId(gaAccountingCheckConfig);
        return BeanMapping.copy(gaAccountingCheckConfig, AccountingCheckConfigRes.class);
    }

    @CacheEvict(key = "'accounting_check_'+#tenantId+'_'+#productId")
    @Override
    public Boolean removeByProIdAndTenId(String tenantId, String productId) throws TxnException {
        if (StringUtils.isBlank(tenantId) ||StringUtils.isBlank(productId)) {
            logger.error("科目表主键ID或租户ID或产品ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int a = extGaAccountingCheckConfigMapper.deleteByTenIdAndProId(productId,tenantId);
        return a > 0;
    }
}
